﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace DownloadPlug_ins
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void pSet_Click(object sender, EventArgs e)
        {
            Config config = new Config();
            config.ShowDialog();
        }

        private void gbtnOk_Click(object sender, EventArgs e)
        {
            string tableName = this.ctxtTableName.Text.Trim();
            string filed = this.ctxtFileName.Text.Trim();
            string post=this.ctxtNumber.Text.Trim();

            int count = 0;
            if (tableName != "" && filed != "" && post != "")
            {
                string sql = "select djlsh,djbth,djsth  from " + tableName + " order by  djlsh";
                DataTable dt = DbHelperSQL.ExecuteDt(sql);

                if (dt != null && dt.Rows.Count > 0)
                {
                    int index=1;
                    foreach (DataRow dr in dt.Rows)
                    {
                        string value = GetNumStr(index, int.Parse(post));
                        string djlsh=dr["djlsh"].ToString();
                        string djbth = dr["djbth"].ToString();
                        string djsth = dr["djsth"].ToString();


                     int ss2=   Update(tableName, filed, value, djlsh, djbth, djsth);
                     if (ss2 > 0)
                         count++;
                            index++;
                    }
                }

                if (count > 0)
                    MessageBox.Show("共计插入条"+count+"数据!");
            }

        }
        private int Update(string table, string filed, string value, string djlsh, string djbth, string djsth)
        {
            string sql = "update " + table + " set " + filed + "='" + value + "' where djlsh=" + djlsh + "  and djbth=" + djbth + " and djsth=" + djsth;
            int ss = DbHelperSQL.ExecuteSql(sql);
            return ss;
        }

        private string GetNumStr(int number,int post)
        {


            if (number.ToString().Length > post)
            {

                return number.ToString();

            }

            else
            {
                return number.ToString().PadLeft(post, '0'); // 一共4位,位数不够时从左边开始用0补
            }

        }
    }
}
